package com.wy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wy.model.Result;
import com.wy.model.Customer;
import com.wy.service.CustomerService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
* @描述
* @创建人 wangyue
* @创建时间2019/12/216:25
*/
@Api("restful风格的api示例")
@RestController
@RequestMapping("customer")
public class CustomerController {

@Autowired
private CustomerService customerService;

@ApiOperation(value = "查询列表")
@GetMapping("list")
public Result<Customer> list1(HttpServletRequest request, Customer customer,
    @RequestParam(defaultValue = "1") int pagenum,
    @RequestParam(defaultValue = "2") int pagesize){
    PageHelper.startPage(pagenum,pagesize);
    List<Customer> customerList = customerService.list(customer);
        PageInfo<Customer> pageInfo = new PageInfo<>(customerList);
        return new Result(pageInfo.getTotal(),pageInfo.getList());
    }
            @ApiOperation(value = "查询单条")
            @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "用户id",required = true,paramType = "path")})
            @ApiResponses({@ApiResponse(code=400,message="id为空")})
            @GetMapping("getById/{id}")
            public Customer getById(@PathVariable String id){
            return customerService.getById(id);
            }


            @ApiOperation(value = "新增修改")
            @PostMapping("save")
            public Boolean save(@RequestBody Customer customer){
            String id = customer.getId();
            if(id !=null && !id.equals("")){
            customerService.update(customer);
            } else{
            customer.setId(UUID.randomUUID().toString());
            customerService.insert(customer);
            }
            return true;
            }
            @ApiOperation(value = "删除")
            @DeleteMapping("delete")
            public boolean delete(String ids){
            if(ids != null && !ids.equals("")){
            customerService.deletebth(ids.split(","));
            }
            return true;
            }
            }
